package com.wing.res.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.enums.MaintenanceEnum;
import com.wing.common.enums.ProductEnum;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.product.model.entity.ProductUnitInfo;
import com.wing.product.service.ProductUnitService;
import com.wing.res.model.entity.ResProductDraw;
import com.wing.res.model.entity.ResProductMoldDraw;
import com.wing.res.model.request.*;
import com.wing.res.model.response.ResProTaskDetailVo;
import com.wing.res.model.response.ResProTaskSearchVo;
import com.wing.res.service.*;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工程规划记录
 * @version 1.1.0
 * @Author hz
 * @Date 2024-09-19 15:36
 **/
@Slf4j
@Validated
@Api(value = "工程规划记录管理", tags = "#/res/planRecord/proPlanRecord")
@RestController("resPlanRecordController")
@RequestMapping("res/admin/planRecord")
public class ResPlanRecordController {

    @Resource
    private ResProTaskService resProTaskService;

    @Resource
    private ResProductDrawService resProductDrawService;

    @ApiOperation(value = "查研发任务列表", notes = "搜索-任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<ResProTaskSearchVo> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,Long productTypeId
            ,Long customerId
            ,String customerProCode
            ,String productStatus
            ,String customerProFigure
            ,String givenStartTimed
    ) {
        Page<ResProTaskSearchVo> pg = new Page<>(page, limit);
        Map map = new HashMap();
        map.put("customerId", customerId);
        map.put("customerProCode", customerProCode);
        map.put("productStatus", productStatus);
        if (StrUtil.isNotEmpty(givenStartTimed)){
            // 使用 split 方法分割字符串
            String[] dates = givenStartTimed.split(" 至 ");
            // 获取开始和结束日期
            String startDate = dates[0];
            String endDate = dates[1];
            map.put("givenStartTime", startDate);
            map.put("givenEndTime", endDate);
        }
        map.put("customerProFigure", customerProFigure);
        map.put("productTypeId", productTypeId);
        String code = MaintenanceEnum.Task_Status_Enum.DGH.getCode();
        Page<ResProTaskSearchVo> list = resProTaskService.pageList4app(pg, map,code);
        list.getRecords().stream().forEach(info-> {
                    info.setProductStatus(MaintenanceEnum.Task_Status_Enum.getNameByCode(info.getProductStatus()));
                    info.setSource(MaintenanceEnum.Task_Source_Enum.getNameByCode(info.getSource()));
                    //如果产品图号为空的话
//                    if (StrUtil.isEmpty(info.getCusProductFigure())){
//                        QueryWrapper<ResProductDraw> queryWrapper = new QueryWrapper<>();
//                        queryWrapper.eq("is_default","1");
//                        queryWrapper.eq("task_id",info.getId());
//                        queryWrapper.eq("product_detail_id",info.getProductDetailId());
//                        ResProductDraw one = resProductDrawService.getOne(queryWrapper);
//                        if (ObjectUtil.isNotEmpty(one)){
////                            info.setCusProductFigure(one.getName());
////                            info.setFileUrl(one.getFileUrl());
//                        }
//                    }
                }
        );
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    /**
     * 确认信息并校对
     * @param id
     * @return
     */
    @ApiOperation(value = "确认信息并校对", notes = "确认信息并校对")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/submitProofread")
    public JsonResult submitProofreadTo(@RequestParam("id") String id,@RequestParam("proofreadResult") String proofreadResult,String remark) {
        if (resProTaskService.submitProofreadTo(id,proofreadResult,remark) > 0) {
            return JsonResult.success("操作成功");
        }
        return JsonResult.fail("操作失败");
    }


    /**
     * 确认信息并审核
     * @param id
     * @return
     */
    @ApiOperation(value = "确认信息并审核", notes = "确认信息并审核")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/submitAudit")
    public JsonResult submitAudit(@RequestParam("id") String id,@RequestParam("auditResult") String auditResult,String remark) {
        if (resProTaskService.submitAudit(id,auditResult,remark) > 0) {
            return JsonResult.success("操作成功");
        }
        return JsonResult.fail("操作失败");
    }

}
